package ro.src;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;

public class Training {
	private ArrayList<Features> fList;
	
	public Training(){
		fList = new ArrayList<Features>();
	}
	
	public void addElement(Features f){
		fList.add(f);
	}
	
	public int findKNN(int k, Features f){
		ArrayList<Features> minList = new ArrayList<Features>();;
		for(int i=0; i<fList.size(); i++){
			fList.get(i).distance = 0;
			float maxdist = 0;
			maxdist = Math.max(maxdist, Math.abs(fList.get(i).ratio-f.ratio));
			maxdist = Math.max(maxdist, Math.abs(fList.get(i).diagonal - f.diagonal));
			maxdist = Math.max(maxdist, Math.abs(fList.get(i).cross - f.cross));
			maxdist = Math.max(maxdist, Math.abs(fList.get(i).quarter1 - f.quarter1));
			maxdist = Math.max(maxdist, Math.abs(fList.get(i).quarter2 - f.quarter2));
			maxdist = Math.max(maxdist, Math.abs(fList.get(i).quarter3 - f.quarter3));
			
			fList.get(i).distance += maxdist;
			
			if(i<k){
				minList.add(fList.get(i));
				Collections.sort(minList);
			}
			else{
				if(fList.get(i).distance<minList.get(k-1).distance){
					
					minList.remove(k-1);
					minList.add(fList.get(i));
					Collections.sort(minList);
				}
			}
			
		}
		
		
		//k najblizej
		
		int selected[]= new int[k];
	
		//Collections.sort(fList);
		for(int i=0; i<k; i++){
			selected[i]=minList.get(i).label;
		}
		
		/*for(int i=0; i<k; i++){
			float value=10000;
			for(int j=0; j<fList.size(); j++){
				if(fList.get(j).distance<value){
					boolean free=true;
					for(int x=0; x<k; x++){
						if(j==selected[x]) free=false;
					}
					if(free) { selected[i]=j; value=fList.get(j).distance; }
				}
			}
		}*/
		//oszacowac
		int result[]= new int[10];
		int count=0, digit=0;
		//zlicz wystapienia cyfr
		for(int i=0; i<k ;i++){
			result[selected[i]]++;
			//result[fList.get(selected[i]).label]++;
		}
		//wybierz ta najliczniejsza
		for(int i=0; i<10; i++){
			if(result[i]>count) {
				digit=i;
				count=result[i];
			}
		}
		return digit;
	}
	
	public boolean getKnowledge(String path){
		System.out.println("Baza - Check");
		GUI.textInfo.append("Baza - Check\n");
		try {
			FileInputStream fstream = new FileInputStream(path);
			DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			String strLine;
			System.out.println("Baza - Reading");
			GUI.textInfo.append("Baza - Reading\n");
			fList.clear();
			while ((strLine = br.readLine()) != null){
				String[] dane = strLine.split(" ");
				Features f = new Features();
				f.ratio = Float.parseFloat(dane[0]);
				f.diagonal = Float.parseFloat(dane[1]);
				f.cross = Float.parseFloat(dane[2]);
				f.quarter1 = Float.parseFloat(dane[3]);
				f.quarter2 = Float.parseFloat(dane[4]);
				f.quarter3 = Float.parseFloat(dane[5]);
				f.label = Integer.parseInt(dane[6]);
				fList.add(f);
			}
			in.close();
			System.out.println("Baza - Reading - Done");
			GUI.textInfo.append("Baza - Reading - Done\n");
			}
			catch (FileNotFoundException io)												
				{ System.out.println("Baza - Missing!!");
				GUI.textInfo.append("Baza - Missing!!\n");
				return true;}
		
			catch (IOException io)												
				{System.out.println(io.getMessage());} 

		return false;
	}
	
	public void writeKnowledge(String path){
		System.out.println("Baza - Writing");
		GUI.textInfo.append("Baza - Writing\n");
		try {
			File file = new File(path);
			file.createNewFile();
			BufferedWriter out = new BufferedWriter(new FileWriter(path));
			for(int i=0; i<fList.size(); i++){
				String txt = new String(
						fList.get(i).ratio + " " +
						fList.get(i).diagonal + " " + 
						fList.get(i).cross + " " + 
						fList.get(i).quarter1 + " " + 
						fList.get(i).quarter2 + " " + 
						fList.get(i).quarter3 + " " + 
						fList.get(i).label + "\n");
				out.write(txt);
			}
			out.close();
			System.out.println("Baza - Writing - Done");
			GUI.textInfo.append("Baza - Writing - Done\n");
		} catch (IOException e) {
			e.printStackTrace();
		}	
		
	}
}